<template>
	<view class="countdown">
		<view class="day" v-if="!isStart">距离开始：</view>
		<view class="day" v-if="isStart">距离结束：</view>
		<view class="day" v-if="d!=0"><text class="text">{{d}}</text>天</view>
		<view class="time" style="margin-left: 9rpx;">{{h<10?'0'+h:h}}</view>
		<text class="text">:</text>
		<view class="time">{{m<10?'0'+m:m}}</view>
		<text class="text">:</text>
		<view class="time">{{s<10?'0'+s:s}}</view>
	</view>
</template>

<script>
	export default {
		props: {
			startTime: {
				type: Date,
			},
			endTime: {
				type: Date,
			},
		},
		data() {
			return {
				timer: null,
				d: 0,
				h: 0,
				m: 0,
				s: 0,
				//秒杀开始标致
				isStart:false
			}
		},
		mounted() {
			this.time()
		},
		beforeDestroy() {
			clearInterval(this.timer);
			this.timer = null;
		},
		computed: {
			sTime() {
				return this.startTime
			},
			eTime() {
				return this.endTime
			}
		},
		watch: {
			endTime() {
				clearInterval(this.timer);
				this.time()
			},
			s(){
				if(this.sTime.getTime() <= new Date().getTime() ){
					this.isStart = true;
				}
			},
			isStart(){
				clearInterval(this.timer);
				this.time()
			}
		},
		methods: {
			time() {
				if(!this.isStart && this.sTime.getTime() <= new Date().getTime() ){
					this.isStart = true;
				}
				let leftTime = this.getDateDiff(this.sTime, this.eTime);
				this.getCountdownTime(leftTime);
			},
			//计算两个时间差
			getDateDiff(startTime, endTime) {
				//判断时间是否开始
				if(this.isStart){
					endTime = new Date(endTime);
					return parseInt((endTime.getTime() - new Date().getTime()) / 1000);
				}else{
					startTime = new Date(startTime);
					return parseInt((startTime.getTime() - new Date().getTime()) / 1000);
				}
			},
			//计算活动结束时间
			getCountdownTime(leftTime) {
				let time = leftTime
				if (time > 0) {
					this.timer = setInterval(() => {
						if (time == 0) {
							clearInterval(this.timer)
							this.h = 0
							this.m = 0
							this.s = 0
							
							if(this.isStart){
								this.$emit('countdownEnd');
							}else{
								this.$emit('countdownStarts');
							}
						} else {
							this.d = parseInt(leftTime / 3600 / 24);
							this.h = parseInt((time / 3600) % 24);
							this.m = parseInt((time / 60) % 60);
							this.s = parseInt(time % 60);
							time--
						}
					}, 1000)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.countdown {
		display: flex;
		align-items: center;
		font-size: 20rpx;
		color: #fff;

		.day {
			font-size: 20rpx;
			color: #606060;
			display: flex;
			align-items: center;
			.text {
				color: #FD3C22;
				font-weight: bold;
			}
		}

		.time {
			padding: 3rpx;
			background-color: #606060;
			border-radius: 4rpx;
			box-sizing: border-box;
		}

		.text {
			font-size: 36rpx;
			font-weight: bolder;
			margin: 0 6rpx;
			color: #606060;
		}
	}
</style>
